﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using GaiNo.Core.Services;
namespace GaiNo.Core.Data
{
    public static class SqlHelper
    {
        public static int ExecuteNonQuery(string query)
        {
            Database db = null;
            int result = -1;
            if (!TransactionManager.IsTransaction)
            {
                db = DatabaseFactory.CreateDatabase();
            }
            else
            {
                db = TransactionManager.GetDatabase();
            }
            DbCommand dbCommand = db.GetSqlStringCommand(query);
            DbTransaction tran = TransactionManager.GetDbTransaction();
            try
            {
                if (tran == null)
                    result = db.ExecuteNonQuery(dbCommand);
                else
                    result = db.ExecuteNonQuery(dbCommand, tran);
            }
            catch (Exception ex)
            {
                Log.Current.ApplicationError("DeleteExistFile", ex.ToString());
                if (tran != null)
                    tran.Rollback();

            }
            return result;
        }
        public static IDataReader ExecuteReader(string query)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(query);
            return db.ExecuteReader(dbCommand);
        }
        public static DataSet ExecuteDataSet(string query)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(query);
            return db.ExecuteDataSet(dbCommand);
        }
        public static object ExecuteScalar(string query)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(query);
            return db.ExecuteScalar(dbCommand);
        }
    }
    public static class TransactionManager
    {
        private static Database _db = null;
        private static bool _isTransaction = false;
        private static DbTransaction _dbTransaction = null;
        public static void BeginTransaction()
        {
            _isTransaction = true;
            if (_db == null)
                _db = DatabaseFactory.CreateDatabase();
            DbConnection conn = _db.CreateConnection();
            conn.Open();
            _dbTransaction = conn.BeginTransaction();
        }
        public static bool EndTransaction()
        {
            _isTransaction = false;
            bool isCommit = false;
            if (_dbTransaction.Connection != null)
            {
                _dbTransaction.Commit();
                isCommit = true;
            }
            _dbTransaction.Dispose();
            _dbTransaction = null;
            _db = null;
            return isCommit;
        }
        internal static Database GetDatabase()
        {
            return _db;
        }
        public static bool IsTransaction
        {
            get { return _isTransaction; }
        }
        internal static DbTransaction GetDbTransaction()
        {
            while (!_isTransaction)
            {
                break;
            }
            return _dbTransaction;
        }
    }
}
